import {createRouter, createWebHistory} from 'vue-router'
import {useUserStore} from "@/stores";

// 动态路由
export const dynamic_routes = [
  {
    path:"list", // 列表页
    name:"list",
    component: ()=>import('@/views/list/list.vue')
  },
  {
    path:"detail", // 详情页
    name:"detail",
    // component: ()=>import('@/views/detail/detail.vue'),
    children: [
      {
        path: "personDetail",
        name: "personDetail",
        component: () => import('@/components/personDetail.vue')
      }
    ]
  },
  {
    path:"manage", // 内容管理页
    name:"manage",
    children: [
      {
        path: 'staff',
        name: 'staff',
        component: () => import('@/components/staff.vue')
      }
    ]
  },
  {
    path:"admin", // 人员管理页
    name:"Admin",
    component: ()=>import('@/views/admin/admin.vue')
  }
]

// 静态路由
const routes = [
  {
    path: '/',
    redirect: {name: 'main'}
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login/login.vue')
  },
  {
    path: '/main',
    name: 'main',
    component: () => import('../views/main/main.vue'),
    meta: { need_login: true }
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('../views/NotFound/NotFound.vue')
  }
]


const router = createRouter({
  history: createWebHistory(),
  routes: routes
})

//路由拦截
router.beforeEach((to, from, next) => {
  /*用户是否登录校验*/
  const { userData } = useUserStore()
  const { need_login = false } = to.meta
  if (need_login && !userData) {
    // 当进入的页面需要登录，则跳转到登录页面
    next({
      name: 'login'
    })
  }else{
    next()
  }
})
export default router